#include "pdfeditortool.h"
#include "pdfconstants.h"
#include "pdfsecurityhandler.h"
#include "pdfwidgetutils.h"
#include "pdfviewersettings.h"
#include "pdfapplicationtranslator.h"
#include "pdfeditormainwindow.h"

#include <QSettings>
#include <QApplication>
#include <QCommandLineParser>
#include <QIcon>

PDFEditorTool* PDFEditorTool::m_instance = nullptr;

PDFEditorTool::PDFEditorTool(QObject* parent)
    : QObject(parent)
    , m_initialized(false)
    , m_mainWindow(nullptr)
    , m_application(nullptr)
{
    initialize();
}

PDFEditorTool::~PDFEditorTool()
{
    if (m_mainWindow) {
        delete m_mainWindow;
        m_mainWindow = nullptr;
    }
}

PDFEditorTool *PDFEditorTool::getInstance()
{
    if (!m_instance) {
        m_instance = new PDFEditorTool();
    }

    return m_instance;
}

void PDFEditorTool::releaseInstance()
{
    if (m_instance) {
        delete m_instance;
        m_instance = nullptr;
    }
}

bool PDFEditorTool::initialize()
{
    if (m_initialized) {
        return true;
    }

    // 创建主窗口但不立即显示
    m_mainWindow = new pdfviewer::PDFEditorMainWindow();

    m_initialized = true;
}

void PDFEditorTool::showMainWindow()
{
    if (m_initialized && m_mainWindow) {
        m_mainWindow->show();
        m_mainWindow->raise();
        m_mainWindow->activateWindow();
    }
}

void PDFEditorTool::hideMainWindow()
{
    if (m_initialized && m_mainWindow) {
        m_mainWindow->hide();
    }
}

void PDFEditorTool::openDocument(const QString &filePath)
{
    if (!m_initialized || !m_mainWindow) {
        return ;
    }

    return m_mainWindow->getProgramController()->openDocument(filePath);
}

void PDFEditorTool::closeDocument()
{
    if (m_initialized && m_mainWindow) {
        m_mainWindow->getProgramController()->closeDocument();
    }
}
